package com.samsung.spen.sshare.main.history;

import java.util.ArrayList;
import java.util.Date;

import com.samsung.spen.sshare.R;
import com.samsung.spen.sshare.R.drawable;
import com.samsung.spen.sshare.main.MainActivity;
import com.samsung.spen.sshare.main.data.History;
import com.samsung.spen.sshare.main.data.HistoryColumns;
import com.samsung.spen.sshare.main.data.SshareDataService;
import com.samsung.spen.sshare.main.sms.Constants;
import com.samsung.spen.sshare.main.sms.SendSMSFragment;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class HistoryDetailActivity extends Activity{
	
	public static final int RESULT_CODE_DELETE_DETAIL = 2;
	public static final int RESULT_CODE_SEND_REQUEST = 3;
	
	private ListView mListView = null;
	private ArrayList<History> mHistories = new ArrayList<History>();
	private HistoryDetailDataAdapter adapter;
	private String phone;
	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.main_activity_history_details);
		
		bindService(new Intent(HistoryDetailActivity.this, SshareDataService.class), mConnection, Context.BIND_AUTO_CREATE);
		
		phone = getIntent().getStringExtra(HistoryColumns.PHONE_NUMBER);
		TextView txtPhone = (TextView)findViewById(R.id.tv_detail_phone);
		txtPhone.setText(phone);
		
		mListView = (ListView) findViewById(R.id.detail_history_list_view);
		
		adapter = new HistoryDetailDataAdapter(HistoryDetailActivity.this);
		mListView.setAdapter(adapter);

	}
	@Override
	protected void onDestroy() {
		if(mConnection!=null){
			
		unbindService(mConnection);
		}
		super.onDestroy();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main_activity_detail_history, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_delete_detail :
			int r = mService.deleteDetailHistory(phone);
			Log.d("BEBE", "delete row :" + r);
			Intent i = getIntent();
			setResult(RESULT_CODE_DELETE_DETAIL, i);
			finish();
			return true;
			
		case R.id.menu_send_request_from_detail :		
			Intent intent = getIntent();
			intent.putExtra(HistoryColumns.PHONE_NUMBER, phone);
			setResult(RESULT_CODE_SEND_REQUEST, intent);
			finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
		
	}
	
	class HistoryDetailDataAdapter extends ArrayAdapter<History>{

		public HistoryDetailDataAdapter(Context context) {
			super(context, R.layout.main_listview_detail_history_item);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			DataHolder holder;
			if(row == null){
				LayoutInflater inflater = getLayoutInflater();
				row = inflater.inflate(R.layout.main_listview_detail_history_item, parent, false);
				holder = new DataHolder(row);
				row.setTag(holder);
			} else {
				holder = (DataHolder) row.getTag();
			}
			holder.setData(mHistories.get(position));
			return row;
		}
		
	}
	
	class DataHolder {
		TextView tvFileName, tvDateTime;
		ImageView ivStatus;
		
		public DataHolder(View view){
			tvFileName = (TextView) view.findViewById(R.id.tv_file_name);
			tvDateTime = (TextView) view.findViewById(R.id.tv_date_time);
			ivStatus = (ImageView) view.findViewById(R.id.img_status);
		}
		
		public void setData(History history){
			tvFileName.setText(history.getFileName());
			tvDateTime.setText(new Date(history.getDateTime()).toString());
			if(history.getStatus()== Constants.STATUS_SEND_OK)
			ivStatus.setImageResource(drawable.ic_sent);
			else if(history.getStatus()== Constants.STATUS_SEND_FAIL)
				ivStatus.setImageResource(drawable.ic_send_fail);
			else if(history.getStatus()== Constants.STATUS_RECEIVE_OK)
				ivStatus.setImageResource(drawable.ic_received);
			else if(history.getStatus()== Constants.STATUS_RECEIVE_FAIL)
				ivStatus.setImageResource(drawable.ic_receive_fail);
		}
	}
	
	private SshareDataService mService;
	private ServiceConnection mConnection = new ServiceConnection() {
	
		@Override
		public void onServiceDisconnected(ComponentName name) {
			mService = null;
			
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
		mService = ((SshareDataService.SshareDataServiceBinder) service).getService();
		Log.d("BEBE", "den day chua?detail");
			mHistories = mService.getHistoryListByPhone(phone);	

		adapter.clear();
		adapter.addAll(mHistories);
		}
	};
}
